<?php
session_start();
require_once("_connect.php");
if(true)
{
	$billcode = "03381";
	$max = "201423";
	$min = "201420";
	if($billcode != "" && $max != "" && $min != "" )
	{
		echo lookupBillcodeMsg($conn, $billcode, $max, $min);	
	}	
}

function lookupBillcodeMsg($conn, $billcode, $max, $min)
{
	$sql = "SELECT *   
			FROM billcodemsg msg WHERE 
			msg.billcode =".$billcode." AND
			(msg.camp BETWEEN ".$min." AND ".$max.") AND 
			msg.active = 'YES'";
			//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if (is_array($row))
	{
		return $row["message"];
	}
	else
	{
		return lookupBillcode($conn, $billcode, $max, $min);
	}	
}

function lookupBillcode($conn, $billcode, $max, $min)
{
    $rows = $_SESSION['ORDER_DETAIL'];
    $exists_id = BillCodeExists($billcode, $rows);
    //print_r($exists_id);
    if (is_array($exists_id))
    {
		$Duplicate = "ซ้ำ! รหัสสินค้านี้ถูกระบุแล้วในรายการสั่งซื้อ :\n";
		$Duplicate = $Duplicate."รหัสสินค้า ".$exists_id['BILLCODE']."\n";
		$Duplicate = $Duplicate."ราคาต่อชิ้น ".$exists_id['PRICE']."บาท\n";
		$Duplicate = $Duplicate."จำนวน ".$exists_id['QTY']."ชิ้น\n";						
	}

	$sql = "SELECT *   
			FROM billcode bill WHERE 
			bill.billcode =".$billcode." AND
			(bill.camp BETWEEN ".$min." AND ".$max.")";
			//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if (is_array($row))
	{
		return $row["BILLDESC"]."|".$row["PRICE"]."|".$row["CAMP"]."|".$Duplicate;
	}
	else
	{
		return "Notfound";
	}			

}

function BillCodeExists($billcode, $array)
{
	if (is_array($array))
	{
		foreach ($array AS $rowNum => $row)
		{
			if ($billcode == $row['BILLCODE']) return $array[$rowNum];
		}			
	}
	return 0;
}
?>